package com.stray.crawl.model.dao;

import java.text.MessageFormat;


public enum RegexPattern {

	http 					("^(https?://)"		, ""						, "http://"), 
	number					("\\d*"				, "({1})"					, "{1}"), 
	string					("[\\w-%]*"			, "({1})"					, "{1}"),
	number_query			("\\d*"				, "({0}=({1})&?)'{'1'}'"	, "{0}={1}"),
	string_query			("[\\w-%]*"			, "({0}=({1})&?)'{'1'}'"	, "{0}={1}"),
	number_query_optional	("\\d*"				, "({0}=({1})&?)?"			, "{0}={1}"),
	string_query_optional	("[\\w-%]*"			, "({0}=({1})&?)?"			, "{0}={1}");
	
	private String pattern;
	private String format;
	private String replace;
	
	RegexPattern(String pattern, String format, String replace) {
		this.pattern = pattern;
		this.format = format;
		this.replace = replace;
	}
	
	public String getReplace(String key, String val) {
		return key == null || val == null ? "" : MessageFormat.format(replace, key, val);
	}

	public String getRegex(String key) {
		return key == null ? pattern : MessageFormat.format(format, key, pattern);
	}
	
	public boolean isQuery() {
		return this == number_query ||
			   this == string_query ||
			   this == number_query_optional ||
			   this == string_query_optional ;
	}
	
}